package org.railage.client.gui.main;

import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

import org.railage.client.Client;
import org.railage.common.dto.UserDTO;

public class MainFrame extends JFrame {
	private static final long serialVersionUID = 4535305399874644605L;

	public MainFrame() {

		// setting components
		setJMenuBar(new MenuBar());
		setContentPane(new MainPanel());

		// setting size and location
		setSize(731, 470);
		setResizable(false);
		setLocationRelativeTo(null);

		// setting title and icon
		setTitle(prepareTitle());
		setIconImage(Toolkit.getDefaultToolkit().getImage(
				getClass().getClassLoader().getResource(GUI.ICON_PATH)));

		// default close operation and visibility
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				GUI.exit();
			}
		});
		setVisible(true);
	}

	public String prepareTitle() {
		UserDTO user = Client.getInstance().getCurrentUser();
		int role = user.getRole();
		StringBuilder title = new StringBuilder();
		title.append(GUI.PROGRAM_NAME).append(" - ").append(user.getName());
		title.append(" ").append(user.getSurname()).append(" ");
		if (role == 0) {
			title.append("(User)");
		} else if (role == 1) {
			title.append("(Manager)");
		}
		return title.toString();
	}
}
